package com.henan.caigou.service.impl;

import com.henan.caigou.entity.Contract;
import com.henan.caigou.mapper.ContractMapper;
import com.henan.caigou.service.IContractService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.henan.caigou.vo.ContractVo;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (Contract)表服务实现类
 *
 * @author Shenqy
 * @since 2025-04-24 12:22:58
 */
@Service
public class ContractServiceImpl extends ServiceImpl<ContractMapper , Contract> implements IContractService {
    @Resource
    private HenanCaigouCrawlerService henanCaigouCrawlerService;
    private static final String BASE_URL = "https://zfcg.henan.gov.cn";

    public void start() throws Exception
    {
        String url = BASE_URL + "/henan/ggcx";
        ContractVo vo = new ContractVo();
        while (true) {
            vo = henanCaigouCrawlerService.start(url);

            if (vo.getList().isEmpty()) {
                break;
            }
            // 详情内容
            vo.getList().forEach(contract -> {
                try {
                    Contract detail = henanCaigouCrawlerService.getDetail(BASE_URL + contract.getUrl());
                    contract.setContent(detail.getContent());
                    contract.setPublishOrgin(detail.getPublishOrgin());
                    contract.setPublishTime(detail.getPublishTime());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
            this.saveBatch(vo.getList());
            if (vo.getNextPage().isEmpty()) {
                break;
            }
            url = vo.getNextPage();
        }
    }
}
